/**
 * File ExtensionFilter.java
 * ---------------------------------------------------------
 *
 * Copyright (C) 2012 David Bauske (david.bauske@googlemail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
 * following conditions:
 *
 * - The above copyright notice and this permission notice shall be included in all copies or substantial portions of
 * the Software.
 * - The origin of the software must not be misrepresented.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * TL;DR: As long as you clearly give me credit for this software, you are free to use as you like, even in commercial
 * software, but don't blame me if it breaks something.
 */
package net.cl.util.files;

import java.io.File;
import java.io.Serializable;

import javax.swing.filechooser.FileFilter;

/**
 * An implementation of the FileFilter interface that allows for easy extension-based
 * file filtering, for example for use in a JFileChooser.
 * 
 * In the presence of {@link javax.swing.filechooser.FileNameExtensionFilter FileNameExtensionFilter}, this 
 * class is rather pointless and can probably be ignored by you.
 *
 * @author David
 */
public class ExtensionFilter extends FileFilter implements Serializable
{
	private static final long	serialVersionUID	= 8349054988090860150L;

	private String[]	extensions;
	private String		description;

	/**
	 * Constructs a new Filter. Expects a list of extensions it should accept.
	 * Automatically generates a human-readable description of the accepted formats.
	 * <h3>Example call</h3>
	 * <p>
	 * 	{@code new ExtensionFilter("bmp", "png", "jpg"); // Accepts all image formats used on the web.}
	 * </p>
	 * @param extensions The extensions to accept. Must be at least one element
	 * 		(a zero-element filter is quite pointless). The case of the extension
	 * 		strings is ignored.
	 * @throws IllegalArgumentException if no extensions are passed.
	 */
	public ExtensionFilter(String... extensions)
	{
		if(extensions == null || extensions.length < 1)
		{
			throw new IllegalArgumentException(
					"A FileChooser that accepts not a single file type does not make sense.");
		}

		this.extensions = new String[extensions.length];
		for(int i = 0; i < extensions.length; ++i)
		{
			this.extensions[i] = extensions[i].toLowerCase();
		}

		StringBuilder desc = new StringBuilder();
		desc.append(extensions[0].toUpperCase());

		for(int i = 1; i < extensions.length; ++i)
		{
			desc.append(" / ").append(extensions[i].toUpperCase());
		}
		description = desc.toString();
	}

	@Override
	public boolean accept(File file)
	{
		String filename = file.getName();
		int dotIndex = filename.lastIndexOf('.');
		if(dotIndex >= 0)
		{
			String ext = filename.substring(dotIndex + 1).toLowerCase();
			for(String s : extensions)
			{
				if(ext.equals(s))
				{
					return true;
				}
			}
			return false;
		}
		else
		{
			return true;
		}
	}

	@Override
	public String getDescription()
	{
		return description;
	}
}